import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.OutputFile;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;

import java.util.Collections;

/**
 * @author chen
 * @description mybatis-plus代码生成器
 */
public class CodeGenerator {

    private final static String URL = "jdbc:mysql://localhost:3306/parking";
    private final static String USERNAME = "root";
    private final static String PASSWORD = "123456";

    public static void main(String[] args) {
        FastAutoGenerator.create(URL, USERNAME, PASSWORD)
                .globalConfig(builder -> {
                    builder.author("chen")
                            .outputDir("C:\\Users\\Administrator\\Desktop\\demo\\generator\\src\\main\\java"); // 生成文件路径
                })
                .packageConfig(builder -> {
                    builder.parent("com.itchen")
                            .entity("entity")
                            .mapper("mapper")
                            .service("service")
                            .serviceImpl("service.impl")
                            .controller("controller")
                            .xml("mappers")
                            .pathInfo(Collections.singletonMap(OutputFile.xml,
                                    "C:\\Users\\Administrator\\Desktop\\demo\\generator\\src\\main\\resources\\mappers")); // XML文件生成路径
                })
                .strategyConfig(builder -> {
                    builder.addInclude("private_charging_tbl") // 表名
                            .addTablePrefix("tab_") // 过滤表前缀
                            .addTableSuffix("_tbl") // 过滤表后缀
                            .entityBuilder()
                            .enableLombok()
                            .enableTableFieldAnnotation() // 添加字段注解
                            .naming(NamingStrategy.underline_to_camel) // 下划线转驼峰
                            .columnNaming(NamingStrategy.underline_to_camel) // 下划线转驼峰
                            .enableFileOverride() // 允许覆盖已有文件
                            .versionColumnName("version") // 乐观锁字段
                            .logicDeleteColumnName("deleted") // 逻辑删除字段
                            .addSuperEntityColumns("id", "create_time", "update_time") // 公共字段
                            .mapperBuilder()
                            .enableMapperAnnotation() // 启用 Mapper 注解
                            .enableBaseResultMap() // 启用 BaseResultMap
                            .enableBaseColumnList() // 启用 BaseColumnList
                            .serviceBuilder()
                            .formatServiceFileName("%sService")
                            .formatServiceImplFileName("%sServiceImpl")
                            .controllerBuilder()
                            .enableRestStyle()
                            .enableHyphenStyle() // 启用驼峰转连字符
                            .formatFileName("%sController");
                })
                .templateEngine(new FreemarkerTemplateEngine())
                .injectionConfig(builder -> {
                    // 添加自定义属性，用于模板中判断是否导入 Swagger
                    builder.beforeOutputFile((tableInfo, objectMap) -> {
                        objectMap.put("swagger", true); // 开启 Swagger 支持
                        objectMap.put("enableCrudMethods", true); // 添加CRUD方法开关
                        // 强制设置 serviceVariable 字段
                        objectMap.put("serviceVariable", tableInfo.getServiceName().toLowerCase());
                    });
                })
                .execute();
    }
}
